package com.zhny.iot.yw.pluginHz;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zhny.aiot.plugin.ICenterFacilities;
import com.zhny.aiot.plugin.IDevice;
import com.zhny.aiot.plugin.model.CommandEntry;
import com.zhny.aiot.plugin.model.DeviceEntity;
import com.zhny.aiot.plugin.model.FactoryType;
import com.zhny.aiot.plugin.model.StoreEntity;
import com.zhny.aiot.plugin.mqtt.MqttDeviceFactory;
import com.zhny.aiot.plugin.core.utils.ConfigUtils;
import com.zhny.iot.yw.pluginHz.config.YWFertilizerMqttConfig;
import com.zhny.iot.yw.pluginHz.model.FertilizerParam;
import com.zhny.iot.yw.pluginHz.model.YWFertilizerStoreEntity;
import org.pf4j.Extension;
import org.pf4j.Plugin;
import org.pf4j.PluginWrapper;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author pengshuai
 * @Date 2025/3/6
 */
public class YWFertilizerMqttPlugin extends Plugin {

    public YWFertilizerMqttPlugin(PluginWrapper wrapper) {
        super(wrapper);
    }

    @Extension
    public static class YWFertilizerMqttFactoryV2 extends MqttDeviceFactory<YWFertilizerMqttConfig> {

        @Override
        public List<CommandEntry> supportCommandClasses(){
            String paramJson;
            ObjectMapper mapper = new ObjectMapper();
            mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
            try {
                paramJson = mapper.writeValueAsString(ConfigUtils.getConfigFields(FertilizerParam.class));
            } catch (Exception e) {
                return null;
            }

            //订阅读主题：A000000001/r
            //订阅写主题：A000000001/w
//            ID 	点位	    读写	类型
//            1.1	出水阀开到位	只读	bool
//            1.2	出水阀关到位	只读	bool
//            1.3	泵故障	    只读	bool
//            1.4	泵运行	    只读	bool
//            1.5	远程就地(1为远程，0为就地)	只读	bool
//            1.6	远程一键启动	读写	bool
//            1.7	远程一键停止	读写	bool
//            1.8	运行时长	    只读	short
//            1.9	设定时长	    读写	short

            List<CommandEntry> commands = new ArrayList<>();
            commands.add(CommandEntry.valueOf("start", "远程一键启动", "无参数",null));
            commands.add(CommandEntry.valueOf("stop", "远程一键停止", "无参数",null));
            commands.add(CommandEntry.valueOf("setRunTimeValue", "设定时长", "有参数",paramJson));
            return commands;
        }

        @Override
        public Class<YWFertilizerMqttConfig> configClass() {
            return YWFertilizerMqttConfig.class;
        }

        @Override
        public IDevice createDevice(ICenterFacilities iCenterFacilities, DeviceEntity deviceEntity) {
            return new YWFertilizerMqttDevice(this,iCenterFacilities,deviceEntity);
        }

        @Override
        public Class<? extends StoreEntity> storeClass() {
            return YWFertilizerStoreEntity.class;
        }


        @Override
        public String getId() {
            return "yw-fertilizer-plugin-v2";
        }

        @Override
        public String getName() {
            return "水肥一体机";
        }

        @Override
        public FactoryType getType() {
            return FactoryType.FertilizerController;
        }
    }

}
